package com.awspaas.user.apps.processforsap.impl.send;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.processforsap.model.MessageHeader;
import com.awspaas.user.apps.processforsap.util.HttpClientUtil;
import com.awspaas.user.apps.processforsap.util.ParentInterface;

import java.util.ArrayList;
import java.util.List;

public class SenderFI004Impl2 extends ParentInterface {

    public SenderFI004Impl2(JSONObject jsonObject) {
        super(jsonObject);
    }

    @Override
    public void dueInterFaceData() {
        String result = "";
        try {
            MessageHeader messageHeader = super.messageHeader;
            JSONObject sendData = super.sendData;
            String msgType = "S";
            String message = "";

            List<String> boidList = new ArrayList<>();

            JSONArray requests = sendData.getJSONArray("Requests");
            for (int i = 0; i < requests.size(); i++) {
                JSONObject jsonObject = requests.getJSONObject(i);
                JSONArray rows = jsonObject.getJSONArray("Rows");
                for (int j = 0; j < rows.size(); j++) {
                    boidList.add(String.valueOf(j));
                }
            }


            BO configBo = messageHeader.getConfigBo();
            String url = configBo.getString("SEND_URL");
            result = HttpClientUtil.doPost(url, sendData.toJSONString(), SDK.getRuleAPI().executeAtScript("@getSapParamas(PONAME)"), SDK.getRuleAPI().executeAtScript("@getSapParamas(POPASSWORD)"));

            JSONObject jsonObject = JSONObject.parseObject(result);
            JSONObject newJsonResult = new JSONObject();
            if (boidList.size() == 1) {
                JSONObject responses = jsonObject.getJSONObject("Responses");
                //封装最终数据
                JSONObject jsonObject1 = new JSONObject();
                String msgtyp = responses.getString("MSGTYP");
                jsonObject1.put("Type", msgtyp);
                jsonObject1.put("MESSAGE", responses.getString("MSGTXT"));
                newJsonResult.put("Responses", jsonObject1);
                result = JSONObject.toJSONString(newJsonResult);
            } else {
                JSONArray responses = jsonObject.getJSONArray("Responses");
                //封装最终数据
                for (int i = 0; i < responses.size(); i++) {
                    JSONObject jsonObject1 = responses.getJSONObject(i);
                    String msgtyp = jsonObject1.getString("MSGTYP");
                    if (msgtyp.equals("S")) {

                    } else {
                        msgType = "E";
                    }
                    message = message + "<br>" + jsonObject1.getString("MSGTXT");
                }
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("Type", msgType);
                jsonObject1.put("MESSAGE", message);
                newJsonResult.put("Responses", jsonObject1);
                result = JSONObject.toJSONString(newJsonResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
            super.resultType = false;
            throw new BPMNError("0331", e.getMessage());
        } finally {
            super.resultSendData = sendData.toJSONString();
            super.returnData = result;
        }

    }

}